package com.itheima.controller.system;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageInfo;
import com.itheima.controller.BaseServlet;
import com.itheima.domain.system.Role;
import com.itheima.service.system.RoleService;
import com.itheima.service.system.impl.RoleServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@WebServlet(urlPatterns = "/system/role")
public class RoleServlet extends BaseServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }


    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        //获取参数
        String operation = request.getParameter("operation");
        if ("list".equals(operation)) {
            //页面显示数据
            list(request, response);
        } else if ("toAdd".equals(operation)) {
            //新建
            toAdd(request, response);
        } else if ("save".equals(operation)) {
            //add.jsp新建页面
            save(request, response);
        } else if ("delete".equals(operation)) {
            //删除功能
            delete(request, response);
        } else if ("toEdit".equals(operation)) {
            //编辑功能
            toEdit(request, response);
        } else if ("edit".equals(operation)) {
            //编辑功能
            edit(request, response);
        } else if ("author".equals(operation)) {
            // 进入授权界面
            author(request, response);
        } else if ("getModuleByRoleId".equals(operation)) {
            // 通过角色id获取模块的数据
            getModuleByRoleId(request, response);
        }else if ("updateRoleModule".equals(operation)) {
            updateRoleModule(request, response);
        }

    }

    private void updateRoleModule(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取参数
        String roleId = request.getParameter("roleId");
        String moduleIds = request.getParameter("moduleIds");

        RoleService roleService = new RoleServiceImpl();
        roleService.updateRoleModule(roleId,moduleIds);



        request.getRequestDispatcher(request.getContextPath() + "/system/role?operation=list").forward(request, response);


    }

    // 2021/10/18 14:39 create start by guowenliang
    // 通过角色id获取模块的数据
    private void getModuleByRoleId(HttpServletRequest request, HttpServletResponse response) {
        String roleId = request.getParameter("id");
//        roleService.getModuleByRoleId(roleId);
    }
    // 2021/10/18 14:39 create end by guowenliang

    // 2021/10/18 14:26 create start by guowenliang
    // 授权功能
    private void author(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Role role = this.findById(request, response);
        String roleId = role.getRoleId();
        request.setAttribute("role", role);
        //根据角色id获取所有的模块数据，并加载数据关系
        RoleService roleService = new RoleServiceImpl();
        //获取授权的数据By角色id
        List<Map> roleModuleJson = roleService.findAuthorDataByRoleId(roleId);
        //System.out.println(roleModuleJson);
        //map转换成json数据
        String json = new ObjectMapper().writeValueAsString(roleModuleJson);
        request.setAttribute("roleModuleJson",json);

        request.getRequestDispatcher(request.getContextPath() + "/WEB-INF/pages/system/role/author.jsp").forward(request, response);
    }
    // 2021/10/18 14:26 create end by guowenliang

    private Role findById(HttpServletRequest request, HttpServletResponse response) {
        //获取参数
        String id = request.getParameter("id");
        //System.out.println(id);
        //调用service层
        RoleService roleService = new RoleServiceImpl();
        Role role = roleService.findRoleById(id);
        return role;
    }

    private void edit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取参数
        String id = request.getParameter("id");
        String name = request.getParameter("name");
        String remark = request.getParameter("remark");
        Role role = new Role();
        role.setRoleId(id);
        role.setName(name);
        role.setRemark(remark);
        System.out.println(role);

        //调用service层
        RoleService roleService = new RoleServiceImpl();
        roleService.updateRole(role);

        request.getRequestDispatcher(request.getContextPath() + "/system/role?operation=list").forward(request, response);
        //list(request,response);
    }

    private void toEdit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Role role = findById(request, response);
        request.setAttribute("role", role);
        request.getRequestDispatcher(request.getContextPath() + "/WEB-INF/pages/system/role/update.jsp").forward(request, response);
    }

    private void delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取参数
        String id = request.getParameter("id");

        //调用service层删除功能
        RoleService roleService = new RoleServiceImpl();
        roleService.deleteRole(id);

        //数据响应
        request.getRequestDispatcher(request.getContextPath() + "/system/role?operation=list").forward(request, response);
    }

    private void save(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取参数
        String name = request.getParameter("name");
        String remark = request.getParameter("remark");
        Role role = new Role();
        role.setName(name);
        role.setRemark(remark);
        //调用service层
        RoleService roleService = new RoleServiceImpl();
        roleService.addRole(role);

        //request.setAttribute("role",role);
        //数据响应
        request.getRequestDispatcher(request.getContextPath() + "/system/role?operation=list").forward(request, response);


    }

    private void toAdd(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 响应数据
        request.getRequestDispatcher(request.getContextPath() + "/WEB-INF/pages/system/role/add.jsp").forward(request, response);
    }

    private void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取参数
        String pageNum = request.getParameter("page");
        //调用service层
        RoleService roleService = new RoleServiceImpl();
        PageInfo page = roleService.findByPage(pageNum);

        //响应数据
        request.setAttribute("page", page);
        //response.setContentType("text/html;charset=utf-8");
        request.getRequestDispatcher(request.getContextPath() + "/WEB-INF/pages/system/role/list.jsp").forward(request, response);

    }
}